package com.sinosoft.brokerchannel.rest;

import com.alibaba.fastjson.JSONObject;
import com.sinosoft.brokerchannel.base.annotation.MyResponseBody;
import com.sinosoft.brokerchannel.base.bins.AppMain;
import com.sinosoft.brokerchannel.base.common.SystemParamFinal;
import com.sinosoft.brokerchannel.base.model.ResponseHead;
import com.sinosoft.brokerchannel.base.rest.AbstractController;
import com.sinosoft.brokerchannel.base.server.LogService;
import com.sinosoft.brokerchannel.base.utile.BusinessException;
import com.sinosoft.brokerchannel.base.utile.HttpclientUtil;
import com.sinosoft.brokerchannel.base.utile.ToolUtils;
import com.sinosoft.brokerchannel.entity.vo.*;
import com.sinosoft.brokerchannel.server.ProposalGenerateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 需求名称：[保险经纪渠道平台]<br/>
 *
 * @author gzj
 * @version V1.0
 * @date 2018-8-21 10:48
 * 核保
 */
@Api(value = "services", description = "车险渠道平台核保")
@Controller
@RequestMapping("/proposal")
public class ProposalGenerateController extends AbstractController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private LogService logService;
    @Autowired
    private ProposalGenerateService proposalGenerateService;

    @RequestMapping(value = "proposalgenerate", method = RequestMethod.POST, produces = {"application/xml;charset=UTF-8", "application/json;charset=UTF-8"}, consumes = {"application/xml;charset=UTF-8", "application/json;charset=UTF-8"})
    @ApiOperation(value = "投保", notes = "投保", position = 3)
    @ApiResponses({@ApiResponse(code = 400, message = "失败")})
    @MyResponseBody
    public ProposalGenerateResponse proposalgenerate(@RequestBody ProposalGenerateRequest request) throws Exception { long firstTime = System.currentTimeMillis();
        ProposalGenerateResponse response = null;// 响应对象
        String flowid = "";//交易ID
        try {
            //初始化
            super.init("投保交易", request.getRequestHead());
            //记录日志
            flowid = super.beforeTheTransaction(request);
            //调用服务
            response = proposalGenerateService.proposalGenerate(request);
            PackageJQVO jqvo2 = new PackageJQVO();
            PackageSYVO syvo2 = new PackageSYVO();
            int num = (int) (Math.random() * 9000) + 1000;
            //假数据
            VhlVO vhlVO = new VhlVO();
            vhlVO.setcAppNo("00200000103000120181357934");
            VhlVO vhlVO1 = new VhlVO();
            vhlVO1.setcAppNo("00200000103301120181423335");
            //假数据
            AppMain appMain = new AppMain();
            appMain.setAppno("00200000103000120181357934");
            appMain.setProdno("030001");
            appMain.setDatasrc("04");
            String string = "2018-07-26 08:45:10";
            String string1 = "2018-07-26 16:00:00";
            String string2 = "2019-07-26 15:59:59";
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            PackageJQVO jqvo = new PackageJQVO();
            PackageSYVO syvo = new PackageSYVO();
            try {
                appMain.setInputdate(sdf.parse(string));
                appMain.setInsrncbgntm(sdf.parse(string1));
                appMain.setInsrncendtm(sdf.parse(string2));
                syvo.settInsrncBgnTm(string1);
                syvo.settInsrncEndTm(string2);
            } catch (Exception e) {

            }
            appMain.setQrycde("01APIC130018001532594695119938");
            appMain.setIrrratio(0.0);
            appMain.setBaseprm(950.0);
            appMain.setPrm(950.0);
            appMain.setNaggtax(150.0);
            appMain.setFundrate(0.0);
            appMain.setFundamount(0.0);
            appMain.setFlowno(0);
            appMain.setFlowstatus(0);
            appMain.setIfuploadimage("0");
            appMain.setChannelcode(request.getRequestBody().getSaleInformation().getChannelAgreeCode());
            appMain.setExtend1("???");
            appMain.setExtend2("???");
            appMain.setExtend3("130000");
            //假数据
            AppMain appMain1 = new AppMain();
            appMain1.setAppno("00200000103301120181423335");
            appMain1.setProdno("030001");
            appMain1.setDatasrc("04");
            String string3 = "2018-07-26 08:45:10";
            String string4 = "2018-07-26 16:00:00";
            String string5 = "2019-07-26 15:59:59";
            try {
                appMain1.setInputdate(sdf.parse(string3));
                appMain1.setInsrncbgntm(sdf.parse(string4));
                appMain1.setInsrncendtm(sdf.parse(string5));
            } catch (Exception e) {

            }
            appMain1.setQrycde("V0101APIC130018001532594696066");
            appMain1.setIrrratio(100.0);
            appMain1.setBaseprm(13032.15);
            appMain1.setPrm(13032.15);
            appMain1.setNaggtax(0.0);
            appMain1.setFundrate(0.0);
            appMain1.setFundamount(0.0);
            appMain1.setFlowno(0);
            appMain1.setFlowstatus(0);
            appMain1.setIfuploadimage("0");
            appMain1.setChannelcode(request.getRequestBody().getSaleInformation().getChannelAgreeCode());
            appMain1.setExtend1("???");
            appMain1.setExtend2("???");
            appMain1.setExtend3("130000");
            //假数据
            syvo2.setcAppNo("00200000103301120181423335");
            syvo2.setcProdNo("033011");
            syvo2.setcQryCde("V0101APIC130018001532594696066");
            syvo2.setcPlyTyp("0");
            syvo2.setVhlVO(vhlVO1);
            syvo2.setFlag("1");
            syvo2.setcUdrMrk("2");
            syvo2.setcUdrMsg("自动核保成功。");
            syvo2.setcInvType("007");
            syvo2.setAppMain(appMain1);
            syvo2.setDataSrcForB2B("1");
            //假数据
            jqvo2.setcAppNo("00200000103000120181357934");
            jqvo2.setcProdNo("030001");
            jqvo2.setcQryCde("01APIC130018001532594695119938");
            jqvo2.setcPlyTyp("0");
            jqvo2.setcTonageNumsFlag("0");
            jqvo2.setDataSrcForB2B("1");
            jqvo2.setFlag("1");
            jqvo2.setcUdrMrk("2");
            jqvo2.setcUdrMsg("自动核保成功。");
            jqvo2.setcInvType("007");
            jqvo2.setcTonageNumsFlag("0");
            jqvo2.setDataSrcForB2B("1");
            jqvo2.setVhlVO(vhlVO);
            jqvo2.setAppMain(appMain);
            ProposalGenerateResponseMain pm = new ProposalGenerateResponseMain();
            pm.setPackageSYVO(syvo2);
            pm.setPackageJQVO(jqvo2);
            String str = "AT2018080210595695" + num;
            pm.setOrderNo(str);
            pm.setVhlVO(vhlVO);
            response.setResponseBody(pm);
//B2B假数
            String sNo = "O0000030001201700" + num;
            //假数据
            CvrgVO cvrgVO = new CvrgVO();
            cvrgVO.setcCvrgNo("030000");
            cvrgVO.setcCustCvrgNme("机动车交通事故强制责任险");
            cvrgVO.setnAmt("122000.0");
            cvrgVO.setcDductMrk("369004");
            cvrgVO.setNrate("0.0");
            cvrgVO.setnBasePrm("950.0");
            cvrgVO.setnBefPrm("950.0");
            cvrgVO.setnDductRate("0.2");
            cvrgVO.setnPrm("950.0");
            cvrgVO.setnLiabDaysLmt("0");
            cvrgVO.setnPerAmt("0.0");
            cvrgVO.setnDeductibleRate("");
            cvrgVO.setnResvNum13("0");
            cvrgVO.setnDductPrm("0");
            cvrgVO.setnBasePurePrm("0");
            CvrgVO cvrgVO1 = new CvrgVO();
            cvrgVO1.setcCvrgNo("033001");
            cvrgVO1.setcCustCvrgNme("车辆损失险");
            cvrgVO.setnAmt("122800.0");
            cvrgVO1.setcDductMrk("369003");
            cvrgVO1.setNrate("0.0");
            cvrgVO1.setnBasePrm("6498.2424");
            cvrgVO1.setnBefPrm("9997.3");
            cvrgVO1.setnDductRate("0.15");
            cvrgVO1.setnPrm("9997.3");
            cvrgVO1.setnLiabDaysLmt("0");
            cvrgVO1.setnPerAmt("0.0");
            cvrgVO1.setNrate032618("0.0");
            cvrgVO1.setnDeductibleRate("");
            cvrgVO1.setnResvNum13("0");
            cvrgVO1.setnDductPrm("1499.6");
            cvrgVO1.setnBasePurePrm("6498.2424");
            cvrgVO1.setnAllPrm("");

            CvrgVO cvrgVO2 = new CvrgVO();
            cvrgVO2.setcCvrgNo("033002");
            cvrgVO2.setcCustCvrgNme("第三者责任险");
            cvrgVO2.setnAmt("300000.0");
            cvrgVO2.setcDductMrk("369003");
            cvrgVO2.setNrate("0.0");
            cvrgVO2.setnBasePrm("867.75");
            cvrgVO2.setnBefPrm("1335.0");
            cvrgVO2.setnDductRate("0.15");
            cvrgVO2.setnPrm("1335.0");
            cvrgVO2.setnLiabDaysLmt("0");
            cvrgVO2.setnPerAmt("0.0");
            cvrgVO2.setNrate032618("0.0");
            cvrgVO2.setnDeductibleRate("");
            cvrgVO2.setnResvNum13("0");
            cvrgVO2.setnDductPrm("200.25");
            cvrgVO2.setnBasePurePrm("0");
            cvrgVO2.setnAllPrm("");

            CvrgVO cvrgVO3 = new CvrgVO();
            cvrgVO3.setcCvrgNo("033018");
            cvrgVO3.setcCustCvrgNme("不计免赔特约险");
            cvrgVO3.setnAmt("0.0");
            cvrgVO3.setcDductMrk("369004");
            cvrgVO3.setNrate("0.0");
            cvrgVO3.setnBasePrm("0");
            cvrgVO3.setnBefPrm("1699.85");
            cvrgVO3.setnDductRate("0");
            cvrgVO3.setnPrm("1699.85");
            cvrgVO3.setnLiabDaysLmt("0");
            cvrgVO3.setnPerAmt("0.0");
            cvrgVO3.setNrate032618("0.0");
            cvrgVO3.setnDeductibleRate("");
            cvrgVO3.setnResvNum13("0");
            cvrgVO3.setnDductPrm("0");
            cvrgVO3.setnBasePurePrm("0");
            cvrgVO3.setnAllPrm("");
            //假数据
            PrmCoefVO pv = new PrmCoefVO();
            pv.setDisengageFlag("1");
            pv.setnChannelAdjustValue("0");
            pv.setnAutonomyAdjustValue("0");
            pv.setnAutoChaCoef("0");
            pv.setnResvNum4("100.0");
            pv.setnResvNum3("100.0");
            pv.setnEdrPrjNo("0");
            pv.setnAddRate("0");
            pv.setnClaimTime("0");
            pv.setcDeprotectionFlag("0");
            pv.setnResvNum("0");
            pv.setnTotalClaimAmount("0.0");
            pv.setnAutoCheCoef("0");
            pv.setcNdiscRsn("01");
            pv.setnTotDisc("100.0");
            //假数据
            PrmCoefVO pv1 = new PrmCoefVO();
            pv1.setDisengageFlag("1");
            pv1.setcAgoClmRec("B31");
            pv1.setnResvNum4("0.0");
            pv1.setnResvNum3("1.0");
            pv1.setcNdiscRsn("04");
            pv1.setnTotDisc("0.0");
            pv1.setnChannelAdjustValue("1.0");
            pv1.setnAutonomyAdjustValue("1.0");
            pv1.setnAutoChaCoef("1.0");
            pv1.setnAddRate("0.35");
            pv1.setnClaimTime("0");
            pv1.setcDeprotectionFlag("0");
            pv1.setnResvNum("0");
            pv1.setnTotalClaimAmount("0.0");
            pv1.setnAutoCheCoef("1.0");

            //假数据
            VhlVO vhlVO2 = new VhlVO();
            vhlVO2.setcProdPlace("0");
            vhlVO2.setcEngNo("213131");
            vhlVO2.setcFrmNo("1G1BL52P7TR115520");
            vhlVO2.setcModelCde("DZAAVD0058");
            vhlVO2.setcModelNme("速腾FV7166G轿车");
            vhlVO2.setcUsageCde("364111001");
            vhlVO2.setcVhlTyp("365001");
            vhlVO2.setnNewPurchaseValue("122800");
            vhlVO2.setcFstRegYm("2018-07-26");
            vhlVO2.setnSeatNum("5");
            vhlVO2.setnTonage("0.0000");
            vhlVO2.setcDisplacementLvl("0");
            vhlVO2.setnDisplacement("1.595");
            vhlVO2.setnActualValue("122800.0");
            vhlVO2.setcTravelAreaCde("0");
            vhlVO2.setcGlassTyp("1");
            vhlVO2.setcDevice1Mrk("0");
            vhlVO2.setcRegVhlTyp("K33");
            vhlVO2.setcEcdemicMrk("0");
            vhlVO2.setcRiskDesc("T001");
            vhlVO2.setcTfiSpecialMrk("");
            vhlVO2.setcBrandId("一汽大众");
            vhlVO2.setcAliasId("DZA1AV");
            //vhlVO.settTransferDate("");
            vhlVO2.setcPlateBrandId("一汽大众");
            vhlVO2.setcPlateModelCde("FV7166G ");
            vhlVO2.setcFleetMrk("0");
            vhlVO2.setnDespRate("0.006");
            //vhlVO.setcVhlPkgNO("");
            vhlVO2.setcNewVhlFlag("1");
            vhlVO2.setnPoWeight("1353");
            vhlVO2.setcFuelType("0");
            vhlVO2.setcRenewalFlag("1");
            vhlVO2.setcInspectorNme("");
            vhlVO2.setcInspectionCde("0");
            //vhlVO.setcInspectRec("");
            vhlVO2.setcLoanVehicleFlag("0");
            vhlVO2.setnResvNum2("122800");
            vhlVO2.setcUseYear("373011");
            vhlVO2.setnNewPurchaseRate("0");
            vhlVO2.setcModelNmeJY("速腾FV7166G 时尚型");
            vhlVO2.setcFamilyNme("速腾");
            //vhlVO.setcCertificateType("");
            //vhlVO.setcCertificateNo("");
            //vhlVO.settCertificateDate("");
            //vhlVO.setnNoDamageYears("");
            vhlVO2.setcPlateRecde("BYQKSUUB0014");
            vhlVO2.setcNoticeType("FV7166G ");
            vhlVO2.setcTradeName("一汽-大众汽车有限公司");
            vhlVO2.setcSeriesKindName("速腾");
            vhlVO2.setnNagoActualValue("122800.0");
            vhlVO2.setcRegOwner(request.getRequestBody().getInsuredVO().getcInsuredNme());
            vhlVO2.setcPlateColor("01");
            vhlVO2.setcSpecialCarFlat("0");
            vhlVO2.setcRiskLevel("0");
            //vhlVO.setcFixedParkCde("");
            //vhlVO.setcBodyColor("");
            vhlVO2.setHfCode("0");
            vhlVO2.setcCurModelNme("速腾FV7166G轿车");
            vhlVO2.setAdjustPOrQ("0");
            vhlVO2.setcCertificateDate("2018-07-26");
            vhlVO2.setcVehicleCode("DZAAVD0058");

            //假数据
            VsTaxVO vsTaxVO = new VsTaxVO();
            vsTaxVO.settTaxEffBgnTm("20180701000000");
            vsTaxVO.settTaxEffEndTm("20181231000000");
            vsTaxVO.setnAnnUnitTaxAmt("300.0");
            vsTaxVO.setnTaxableAmt("150.0");
            vsTaxVO.setnLastYear("0.0");
            vsTaxVO.setnOverdueAmt("0.0");
            vsTaxVO.setcTaxUnit("300.0");
            vsTaxVO.setnAggTax("150.0");
            vsTaxVO.setcTaxdptVhltyp("3T1202");
            vsTaxVO.setcPaytaxTyp("03");
            vsTaxVO.setcVsTaxMrk("02");
            vsTaxVO.setcTaxableTyp("1");
            vsTaxVO.setcAbateProp("0.0");
            vsTaxVO.setcAbateAmt("0.0");
            vsTaxVO.setnTaxDue("0.0");
            //假数据
            BaseVO baseVO = new BaseVO();
            baseVO.setcBrkrCde(request.getRequestBody().getSaleInformation().getSalesmanCode());
            baseVO.setcOprCde(request.getRequestBody().getSaleInformation().getSalesmanCode());
            baseVO.setcChaType("0101");
            baseVO.setcSlsId("600097");
            baseVO.setcBrkSlsCde(request.getRequestBody().getSaleInformation().getSalesmanCode());
            baseVO.setcApproveCde("000255");
            baseVO.setcDataSrc("04");
            baseVO.setcAreaFlag("130100");
            baseVO.setcCountryFlag("130110");
            baseVO.setcImmeffMrk("0");
            baseVO.setcProvince("130000");
            baseVO.setcResvTxt9("DC000106");
            baseVO.setcResvTxt10("不不不");
            baseVO.setcResvTxt18("0");
            baseVO.setcPlyNo(sNo);
            baseVO.setcSlsCde(request.getRequestBody().getSaleInformation().getSalesmanCode());
            baseVO.setChannelCode(request.getRequestBody().getSaleInformation().getChannelAgreeCode());
            baseVO.setcDptCde(request.getRequestBody().getSaleInformation().getOrgEname());
            //假数据
            InsuredVO insuredVO = new InsuredVO();
            insuredVO.setcInsuredNme(request.getRequestBody().getInsuredVO().getcInsuredNme());
            insuredVO.setcClntAddr(request.getRequestBody().getInsuredVO().getcResidAddr());
            insuredVO.setcCertfCls(request.getRequestBody().getInsuredVO().getcCertfCls());
            insuredVO.setcCertfCde(request.getRequestBody().getInsuredVO().getcCertfCde());
            //假数据
            VhlownerVO vhlownerVO = new VhlownerVO();
            vhlownerVO.setcOwnerNme(request.getRequestBody().getVhlownerVO().getcOwnerNme());
            vhlownerVO.setcCertfCls(request.getRequestBody().getVhlownerVO().getcCertfCls());
            vhlownerVO.setcCertfCde(request.getRequestBody().getVhlownerVO().getcCertfCde());
            vhlownerVO.setcResidAddr(request.getRequestBody().getVhlownerVO().getcResidAddr());
            ArrayOfCvrgVO aav = new ArrayOfCvrgVO();
            ArrayOfCvrgVO aav1 = new ArrayOfCvrgVO();
            List<CvrgVO> list = new ArrayList<CvrgVO>();
            List<CvrgVO> list1 = new ArrayList<CvrgVO>();
            list.add(cvrgVO);
            list1.add(cvrgVO1);
            list1.add(cvrgVO2);
            list1.add(cvrgVO3);
            aav.setCvrgList(list);
            aav1.setCvrgList(list1);
            //假数据
            String ChannelAgreeCode =  request.getRequestBody().getSaleInformation().getChannelAgreeCode();
            syvo.setcAppNo("00200000103301120181423164");
            syvo.setcProdNo("033011");
            syvo.setcQryCde("V0101APIC130018001532540174419");
            syvo.setFeeProp("0");
            syvo.setnIrrRatio("100.0");
            syvo.setnBasePrm("13032.15");
            syvo.setnPrm("13032.15");
            syvo.setFlag("1");
            syvo.setChannelCode(request.getRequestBody().getSaleInformation().getChannelAgreeCode());
            syvo.setMoneyFreshFlg("1");
            syvo.setBaseVO(baseVO);
            syvo.setPrmCoefVO(pv1);
            syvo.setVhlVO(vhlVO2);
            syvo.setVsTaxVO(vsTaxVO);
            syvo.setInsuredVO(insuredVO);
            syvo.setVhlownerVO(vhlownerVO);
            syvo.setArrayOfCvrgVO(aav1);
            syvo.setcProdNo("030001");
            syvo.setcAppNo("00200000103000120181357934");
            syvo.setAppMain(appMain1);
            //假数据
            jqvo.setnFundRate("0.0");
            jqvo.setnFundAmount("0.0");
            jqvo.setnIrrRatio("0.0");
            jqvo.setnBasePrm("950.0");
            jqvo.setnPrm("950.0");
            jqvo.setFlag("1");
            jqvo.setChannelCode(request.getRequestBody().getSaleInformation().getChannelAgreeCode());
            jqvo.setcInvType("1");
            jqvo.setAppStatus("1");
            jqvo.setcHeckCode("1");
            jqvo.setMoneyFreshFlg("1");
            jqvo.setcAppValidateNo("12354");
            jqvo.setcImmeffMrk("1");
            jqvo.settInsrncBgnTm("20180727000000");
            jqvo.setcAppNo("00200000103000120181357882");
            jqvo.setcProdNo("030001");
            jqvo.settInsrncEndTm("20190726235959");
            jqvo.setcPlyTyp("1");
            jqvo.setFeeProp("0");
            jqvo.setcQryCde("01APIC130018001532588249922254");
            jqvo.setBaseVO(baseVO);
            jqvo.setPrmCoefVO(pv);
            jqvo.setVhlVO(vhlVO2);
            jqvo.setVsTaxVO(vsTaxVO);
            jqvo.setInsuredVO(insuredVO);
            jqvo.setVhlownerVO(vhlownerVO);
            jqvo.setArrayOfCvrgVO(aav);
            jqvo.setcProdNo("030001");
            jqvo.setcAppNo("00200000103000120181357934");
            jqvo.setAppMain(appMain);
            CarQuotePriceResponse response1 = new CarQuotePriceResponse();
            CarQuotePriceResponseMain responseBody = new CarQuotePriceResponseMain();
            responseBody.setPackageSYVO(syvo);
            responseBody.setPackageJQVO(jqvo);
            response1.setResponseBody(responseBody);
            String jsonObject = ToolUtils.obj2json(response1);
            System.out.println(jsonObject);
           try {
               Map<String, String> map = new HashMap<String, String>();
               map.put("pageName", "Account-Add");
               map.put("formName", "AccountAddImpl");
               map.put("userKeys", "portal-v1.0");
               map.put("doCode", "doService");
               map.put("dataJson", jsonObject);
               String url = "http://127.0.0.1:10002/client/service/";
               str = HttpclientUtil.doPost(url, map, "utf-8", true);
               JSONObject json = JSONObject.parseObject(str);
               if("T".equals(json.getString("flag"))){

               }else{
                   throw new BusinessException("0056", "业务数据存储失败！");
               }
           } catch (Exception e) {
               throw new BusinessException("0056", "业务数据存储失败！");
           }

            super.sucessAction();
        } catch (BusinessException e) {
            logger.info(SystemParamFinal.Exception_LOG, e);
            super.businessExceptionAction(e);
        } catch (Exception e) {
            super.exceptionAction(e);
            logger.info(SystemParamFinal.Exception_LOG, e);
        } finally {
            if (response == null) {
                response = new ProposalGenerateResponse();
            }
            ResponseHead rh = new ResponseHead();
            rh.setResponseCode("0");
            rh.setErrorCode("0000");
            rh.setErrorMessage("成功！");
            rh.setEsbCode("00");
            rh.setEsbMessage("成功！");
            response.setResponseHead(rh);
//           monitorService.proposalGenerate(request, response, firstTime);
            //回写日志
            super.afterTheTransaction(response, flowid);
            //输出格式化日志-用于日志易提取信息
//            interfaceLogService.channelToChioce(interfaceLogCollect("ProposalGenerateService", request, response, System.currentTimeMillis() - firstTime));
        }
        //打印调用该服务耗时
        logService.timeConsuming("selfInspection-000003", this, "proposalgenerate", firstTime);
        return response;
    }
}
